package com.wedo.mall.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.utils.EUtils;
import com.wedo.mall.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * Created by lxy on 2017/6/18.
 */

public class ForgotActivity extends EActivity {

    @BindView(R.id.et_username) EditText et_username;
    @BindView(R.id.et_password) EditText et_password;
    @BindView(R.id.et_check_code) EditText et_check_code;
    @BindView(R.id.btn_check_code) Button btn_check_code;

    @BindString(R.string.register_vc_resend) String register_vc_resend;

    @Override
    protected int layout() {
        return R.layout.activity_forgot;
    }

    @Override
    protected void onActivityCreated(Bundle state) {

    }

    /**获取验证码*/
    @OnClick(R.id.btn_check_code)
    protected void onCheckCodeClick(){

        String account = et_username.getText().toString();
        if(!validation(account)) {
            return;
        }
        loading();
        Observable<EResponse> observable = EClient.with().api().verifyCode(account,"1");
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                mTimer.start();
                dismiss(R.string.register_vc_send);
//                String code = response.string("code");
//                et_check_code.setText(code);
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });

    }

    @OnClick(R.id.btn_reset)
    protected void onResetClick(){

        String account = et_username.getText().toString();

        if (!validation(account)){
            return;
        }

        String password = et_password.getText().toString();
        if (TextUtils.isEmpty(password)){
            toast(R.string.register_password_enter);
            return;
        }

        if (password.length() <6 || password.length() >12){
            toast(R.string.register_password_hint);
            return;
        }

        String code = et_check_code.getText().toString();
        if (TextUtils.isEmpty(code)){
            toast(R.string.register_vc_hint);
            return;
        }

        Map<String,String> params = new HashMap<>(5);
        params.put("account",account);
        params.put("password",password);
        params.put("verify",code);

        loading();
        post(EApi.LOGIN_FORGOT_PASSWORD, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss(R.string.fp_reset_successfully);
                defaultFinish();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });


    }

    /**验证注册账号*/
    private boolean validation(String account){


        if(TextUtils.isEmpty(account)) {
            toast(R.string.fp_account_hint);
            return false;
        }

        if (!EUtils.isEmail(account)){
            toast(R.string.fp_account_invalid);
            return false;
        }

//        if (!EUtils.isMobile(account)){
//            if (!EUtils.isEmail(account)){
//                toast(R.string.fp_account_invalid);
//                return false;
//            }
//        }

        return true;
    }

    private CountDownTimer mTimer = new CountDownTimer(60*1000,1000) {
        @Override
        public void onTick(long millis) {
            if (isFinishing() || isDestroyed()){
                return;
            }
            btn_check_code.setClickable(false);
            btn_check_code.setText(String.format(register_vc_resend,millis/1000));
        }

        @Override
        public void onFinish() {
            if (isFinishing() || isDestroyed()){
                return;
            }
            btn_check_code.setClickable(true);
            btn_check_code.setText(R.string.register_get_vc);

        }
    };

    @Override
    protected void onDestroy() {
        mTimer.cancel();
        mTimer = null;
        super.onDestroy();
    }
}
